package com.jiazhong.basic.pack0309;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

// Collection--->List---->LinkedList
public class DemoE {
    // 1. 如何产生对象
    private static void a() {
        LinkedList<Integer> list1 = new LinkedList<>();
        LinkedList<String> list2 = new LinkedList<>();
        LinkedList<Integer> list3 = new LinkedList<>(list1);
        System.out.println(list1);
        System.out.println(list2);
        System.out.println(list3);
    }

    // 2. 如何对集合进行CRUD(存放、获取、修改、删除)
    private static void b() {
        LinkedList<String> list = new LinkedList<>();
        list.add("aa");
        list.add("bb");
        list.add("aa");
        list.add("cc");
        // 放到第一个
        list.push("dd");
        list.add(1, "kk");
        System.out.println(list);
        String s1 = list.get(1);
        String s2 = list.get(2);
        // 取出第一个
        String pop = list.pop();
        // 获取第一个
        String peek = list.peek();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(pop);
        System.out.println(peek);
        list.set(0, "ll");
        System.out.println(list);
        list.remove("pp");
        list.remove(3);
        System.out.println(list);
        list.clear();
        System.out.println(list);
    }

    // 3. 其他方法
    private static void c() {
        LinkedList<String> list = new LinkedList<>();
        list.add("aa");
        list.add("bb");
        list.add("aa");
        list.add("cc");
        int size = list.size();
        System.out.println(size);
        Object[] array = list.toArray();
        System.out.println(Arrays.toString(array));
        boolean empty = list.isEmpty();
        System.out.println(empty);
    }

    // 4. 循环集合
    private static void d() {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.push((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        list.forEach(e -> System.out.print(e + "\t"));
        System.out.println();
        for (Integer e : list) {
            System.out.print(e + "\t");
        }
    }

    // 5. 如何排序、如何查找
    private static void e1() {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.push((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        Collections.sort(list);
        System.out.println(list);
    }

    private static Scanner scan = new Scanner(System.in);

    private static void e2() {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.push((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        System.out.print("input:");
        int num = scan.nextInt();
        boolean b = list.contains(num);
        int index1 = list.indexOf(num);
        int index2 = list.lastIndexOf(num);
        System.out.println(b);
        System.out.println(index1);
        System.out.println(index2);
    }
    // 6. 特有方法
    private static void f(){
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.push((int) (Math.random() * 10 + 1));
        }
        System.out.println(list);
        list.addFirst(99); // push
        list.addLast(23);  // add
        System.out.println(list);
        Integer first = list.getFirst();
        Integer last = list.getLast();
        System.out.println(first);
        System.out.println(last);
        list.removeFirst();
        list.removeLast();
        System.out.println(list);
    }
    public static void main(String[] args) {
        f();
    }
}
